package com.basemanage.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.basemanage.core.model.Condition;
import com.basemanage.core.model.Operators;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.model.SmStore;
import com.basemanage.model.base.BaseSmStore;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@SuppressWarnings("serial")
public class StoreService extends BaseSmStore<SmStore> {

	public static final SmStore dao = new SmStore();

	public static final StoreService me = new StoreService();
	
	/**
	 * 保存/更新
	 * @param wxUser
	 * @return
	 */
	public InvokeResult saveStore(SmStore store) {
		if (store.getInt("id") != null) {
			store.setUpdatetime(new Date());
			store.update();
		} else {
			store.setAddtime(new Date());
			store.save();
		}
		return InvokeResult.success();
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	public InvokeResult delStore(String ids) {
		List<Integer> idsList = new ArrayList<Integer>();

		for (String idstr : ids.split(",")) {

			if (StrKit.isBlank(idstr))
				continue;

			idsList.add(Integer.valueOf(idstr));
		}

		Set<Condition> conditions = new HashSet<Condition>();
		conditions.add(new Condition("id", Operators.IN, idsList));
		dao.delete(conditions);

		return InvokeResult.success();
	}
	
	/*
	 * 修改用户状态
	 */
	public InvokeResult changeState(Integer id,Integer currentState){

	    Boolean success = StoreService.dao.findById(id).set("state",currentState).update();
	    if (success) {
			return InvokeResult.success();
		}else {
			return InvokeResult.failure("修改失败");
		}

	}


	/**
	 * 列表显示
	 * @param ids
	 * @return
	 */
	public Page<SmStore> getPage(int page, int rows, LinkedHashMap<String, String> orderby) {
		return dao.getPage(page, rows,orderby);
	}
	
	/*
	 * 门店显示选择
	 */
	public List<SmStore> getStorelist(){
		return dao.find("select id,title from sm_store");
	}
	



}